package com.hanhai.zrb.api.biz.service.impl;



import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.annotation.Resource;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hanhai.zrb.api.biz.dao.mybatis.FeedbackMapper;
import com.hanhai.zrb.api.biz.service.FeedbackService;
import com.hanhai.zrb.model.user.Feedback;
import com.hanhai.zrb.model.user.dto.FeedbackDto;

@Service("feedbackService")
public class FeedbackServiceImp implements FeedbackService {
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Resource
	private FeedbackMapper feedbackMapper;

	@Override
	public Feedback selectByPrimaryKey(Long id) {
		// TODO Auto-generated method stub
		return feedbackMapper.selectByPrimaryKey(id);
	}

	@Override
	public boolean deleteByPrimaryKey(Long id) {
			return 1==feedbackMapper.deleteByPrimaryKey(id);
	}

	@Override
	public boolean insert(Feedback record) {
			return 1==feedbackMapper.insert(record);
	}

	@Override
	public boolean insertSelective(Feedback record) {
		return 1==feedbackMapper.insertSelective(record);
	}

	@Override
	public boolean updateByPrimaryKeySelective(Feedback record) {
			return	1==feedbackMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public boolean updateByPrimaryKey(Feedback record) {
			return 1 == feedbackMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public PageInfo searchFeedbackPage(FeedbackDto feedbackDto, int pageNum,
			int pageSize) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum, pageSize, true);
		List<Feedback> list = searchFeedbackList(feedbackDto);
		PageInfo pageInfo=new PageInfo(list);
		return pageInfo;
	}
	
	
	@Override
	public List<Feedback> searchFeedbackList(FeedbackDto feedbackDto) {
		if(null!=feedbackDto.getKeyword()){
			feedbackDto.setKeyword("%"+feedbackDto.getKeyword()+"%");
		}
		return feedbackMapper.findBySearch(feedbackDto);
	}
	
	@Override
	public HSSFWorkbook export(List<Feedback> list) {
		   String[] excelHeader = { "唯一ID", "用户名", "联系方式","意见内容","提交方式","提出时间","状态","描述原因"};      
		        HSSFWorkbook wb = new HSSFWorkbook();    
		        HSSFSheet sheet = wb.createSheet("Campaign");    
		        HSSFRow row = sheet.createRow((int) 0);    
		        HSSFCellStyle style = wb.createCellStyle();    
		        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);    
		    
		        for (int i = 0; i < excelHeader.length; i++) {    
		            HSSFCell cell = row.createCell(i);    
		            cell.setCellValue(excelHeader[i]);    
		            cell.setCellStyle(style);    
		            sheet.autoSizeColumn(i);    
		        }    
		    
		        for (int i = 0; i < list.size(); i++) {    
		            row = sheet.createRow(i + 1);    
		            Feedback feedback = list.get(i);    
		            row.createCell(0).setCellValue(feedback.getId());    
		            row.createCell(1).setCellValue(feedback.getNames());    
		            row.createCell(2).setCellValue(feedback.getContact());  
		            row.createCell(3).setCellValue(feedback.getContents());   
		            row.createCell(4).setCellValue(feedback.getSubType());   
		            row.createCell(5).setCellValue(DateFormat(feedback.getCtime()));
		            if(feedback.getStatus()==1){
		            	row.createCell(6).setCellValue("已受理");
		            }else if(feedback.getStatus()==0){
		            	row.createCell(6).setCellValue("未受理");
		            }else{
		            	row.createCell(6).setCellValue("未受理");
		            }
		            row.createCell(7).setCellValue(feedback.getDescription());
		            
		        }    
	
		       
		return wb;
	}
	
	public static String DateFormat(Date dateStr) {
		String str = "";
		if(!"".equals(dateStr) && null!=dateStr){
			try {
				 SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
				 Date date = (Date) sdf.parse(dateStr.toString());
				 SimpleDateFormat sdfs=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		           str=sdfs.format(date);
				 return str;
			} catch (Exception e) {
			}

		}

		 
		 return str;
	}
}
